package com.example.mrwang.fullscreen;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.Toast;

/**
 * Created by MrWang on 2017/1/6.
 */
public class FullFragment extends Fragment implements View.OnClickListener, View.OnTouchListener, Animator.AnimatorListener, ValueAnimator.AnimatorUpdateListener {
    private ViewWrapper mWrapper;
    private LinearLayout mLinFull;
    private Activity mActivity;
    private Button mBtn;
    private FrameLayout mLayou;

    private AnimatorSet mSet;
    private ObjectAnimator mScaleAnimWidth;
    private ObjectAnimator mScaleAnimHeight;
    private boolean mFla = true;
    private int height;
    private int width;
    private int widthPx;
    private int heightPx;
    float down;
    float up;
    private final int popHeight = 500;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mActivity = activity;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        /* widthPx = outMetrics.widthPixels;
        heightPx = outMetrics.heightPixels;*/
        View view = inflater.inflate(R.layout.fragment_full, container, false);

        mLinFull = (LinearLayout) view.findViewById(R.id.lin_full);
        mBtn = (Button) view.findViewById(R.id.btn);
        mLayou = (FrameLayout) mActivity.findViewById(R.id.content);
        ViewGroup.LayoutParams layoutParams = mLinFull.getLayoutParams();

        WindowManager manager = mActivity.getWindowManager();
        DisplayMetrics outMetrics = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(outMetrics);

        mWrapper = new ViewWrapper(mLinFull);
        widthPx = outMetrics.widthPixels;
        heightPx = popHeight;
        width = layoutParams.width;
        height = layoutParams.height;

        mBtn.setOnClickListener(this);

        mLinFull.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        down = event.getY();
                        Log.d("onTouchs", "onTouch: down"+down);

                    case MotionEvent.ACTION_MOVE:
                        float move = event.getY();
                        Log.d("onTouchs", "onTouch: move"+move);
                        Log.d("onTouchs", "onTouch: =="+(move-down));
                        if (move - down > 35) {
                            if (mFla == false) {
                                reSetAniamation();
                            }
                        }

                    case MotionEvent.ACTION_UP:
                        break;
                }
                return true;
            }
        });
        view.setOnTouchListener(this);
        return view;
    }

    private void deFormation(final ViewWrapper wrapper, int widthPx, int heightPx) {
        mScaleAnimWidth = ObjectAnimator.ofInt(wrapper, "width", widthPx);
        mScaleAnimHeight = ObjectAnimator.ofInt(wrapper, "height", heightPx);
        mSet = new AnimatorSet();
        mSet.addListener(this);
        mScaleAnimWidth.addUpdateListener(this);
        mSet.setDuration(250);
        //这里不需要对width做效果
        mSet.play(mScaleAnimHeight);
        mSet.start();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mScaleAnimHeight.cancel();
        mScaleAnimHeight.cancel();
        mSet.cancel();
    }

    private void reSetAniamation() {
        mLayou.setBackgroundDrawable(new ColorDrawable(0x00000000));
        Toast.makeText(mActivity, "还原", Toast.LENGTH_SHORT).show();
        deFormation(mWrapper, width, height);
        mFla = true;
    }

    private void playAniamation() {
        mLayou.setBackgroundDrawable(new ColorDrawable(0xFF666666));
        Toast.makeText(mActivity, "放大", Toast.LENGTH_SHORT).show();
        deFormation(mWrapper, widthPx, heightPx);
        mFla = false;
    }

    @Override
    public void onClick(View v) {
        if (mFla) {
            playAniamation();
        } else {
            reSetAniamation();
        }
    }

    @Override
    public void onAnimationUpdate(ValueAnimator animation) {
        Log.d("tag", "onAnimationUpdate: " + animation.getAnimatedValue());
    }

    @Override
    public void onAnimationStart(Animator animation) {
        Log.d("tag", "onAnimationStart: " + animation.getStartDelay());
    }

    @Override
    public void onAnimationEnd(Animator animation) {

    }

    @Override
    public void onAnimationCancel(Animator animation) {
        Toast.makeText(mActivity, "cancel", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onAnimationRepeat(Animator animation) {

    }

    //popHeight以外区域 重置动画
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int viewMeasuredHeight = mLinFull.getHeight();
        Toast.makeText(getActivity(), event.getY() + mBtn.getHeight() + "", Toast.LENGTH_LONG).show();
        if (event.getY() <= heightPx + mBtn.getHeight()) {
            if (null != mSet) {
                reSetAniamation();
            }
        }
        return false;
    }


    //代理对象
    private static class ViewWrapper {
        private LinearLayout mTarget;

        //        LinearLayout mLinFull;
        public ViewWrapper(LinearLayout target) {
            mTarget = target;
        }


        public int getWidth() {
            return mTarget.getLayoutParams().width;
        }

        public int getHeight() {
            return mTarget.getLayoutParams().height;
        }

        public void setWidth(int width) {
            mTarget.getLayoutParams().width = width;
            mTarget.requestLayout();
        }

        public void setHeight(int height) {
            mTarget.getLayoutParams().height = height;
            mTarget.requestLayout();
        }

        public void setClearManager() {
            FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
            lp.setMargins(0, 0, 0, 0);
            mTarget.setLayoutParams(lp);
        }
    }


}



     /*  *//*layoutParams.width = width2;
        layoutParams.height = height2;
        mLinFull.setLayoutParams(layoutParams);*//*

        ScaleAnimation animation = new ScaleAnimation(mActivity,null);
        animation.initialize(layoutParams.width,layoutParams.height,width2,height2);
        animation.setDuration(500);
        animation.setFillAfter(true);
        mLinFull.startAnimation(animation);

        //animation.startNow();*/
